package com.hui.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.hui.dto.*;
import com.hui.entity.Assets;
import com.hui.entity.AssetsBorrow;
import com.hui.excel.AssetsBorrowExcel;
import com.hui.excel.AssetsExcel;
import com.hui.result.Result;
import com.hui.service.AssetsBorrowService;
import com.hui.service.AssetsService;
import com.hui.util.EasyExcelUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import org.springframework.stereotype.Repository;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "资产申请模块")
@RestController
@RequestMapping("/api/v1/assetsBorrow")
public class AssetsBorrowController {
    @Resource
    private AssetsBorrowService assetsBorrowService;

    @Operation(summary = "新增 - 单条新增")
    @PostMapping("insert")
    public Result<Integer> insert(@RequestBody @Validated AssetsBorrowInsertDTO dto){
        return new Result<>(assetsBorrowService.insert(dto));
    }

    @Operation(summary = "查询 - 单条查询")
    @GetMapping("select/{id}")
    public Result<AssetsBorrow> select(@PathVariable("id") Long id){
        return new Result<>(assetsBorrowService.select(id));
    }


    @Operation(summary = "查询 - 全部记录")
    @GetMapping("list")
    public Result<List<AssetsBorrow>> list(){
        return new Result<>(assetsBorrowService.list());
    }

    @Operation(summary = "查询 - 分页查询")
    @GetMapping("page")
    public Result<PageInfo<AssetsBorrow>> page(@Validated AssetsBorrowPageDTO dto){
        return new Result<>(assetsBorrowService.page(dto));
    }

    @Operation(summary = "修改 - 单条修改")
    @PutMapping("update")
    public Result<Integer> update(@RequestBody @Validated AssetsBorrowUpdateDTO dto){
        return new Result<>(assetsBorrowService.update(dto));
    }

    @Operation(summary = "删除 - 单条删除")
    @DeleteMapping("delete/{id}")
    public Result<Integer> delete(@PathVariable("id") Long id){
        return new Result<>(assetsBorrowService.delete(id));
    }

    @Operation(summary = "删除 - 批量删除")
    @DeleteMapping("deleteBatch")
    public Result<Integer> deleteBatch(@RequestParam("ids") List<Long> ids){
        return new Result<>(assetsBorrowService.deleteBatch(ids));
    }

    @Operation(summary = "查询-报表打印")
    @SneakyThrows
    @GetMapping("/excel")
    public void excel(HttpServletResponse resp){
        List<AssetsBorrowExcel> assetsBorrowExcels = assetsBorrowService.list()
                .stream()
                .map(assetsBorrow -> {
                    AssetsBorrowExcel assetsBorrowExcel = new AssetsBorrowExcel();
                    BeanUtil.copyProperties(assetsBorrow,assetsBorrowExcel);
                    if (ObjectUtil.isNotNull(assetsBorrow.getAssets())){
                        assetsBorrowExcel.setAssetsTitle(assetsBorrow.getAssets().getTitle());
                    }
                    if (ObjectUtil.isNotNull(assetsBorrow.getEmp())){
                        assetsBorrowExcel.setEmpUsername(assetsBorrow.getEmp().getRealname());
                    }
                    assetsBorrowExcel.setIsReturn(ObjectUtil.isNotNull(assetsBorrow.getReturnTime())?"是":"否");
                    return assetsBorrowExcel;
                })
                .collect(Collectors.toList());
        EasyExcelUtil.download(resp,"资产申请统计表",assetsBorrowExcels);
    }


}
